package com.studyyoun.android.nonghe.home.fragment;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.studyyoun.android.androidcommonbaselibrary.fragment.CommonBaseFragment;
import com.studyyoun.android.androidcommonbaselibrary.utils.ToastUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.home.activity.SettingActivity;
import com.studyyoun.android.nonghe.home.item.ItemHeader;
import com.studyyoun.android.nonghe.home.item.ItemJpushMsg;
import com.studyyoun.android.nonghe.person.activity.MyArtReadHistoryActivity;
import com.studyyoun.android.nonghe.person.activity.MyCollectActivity;
import com.studyyoun.android.nonghe.person.activity.MyVideoReadHistoryActivity;
import com.studyyoun.android.nonghe.welcom.App;
import com.studyyoun.android.nonghe.welcom.CommonUserHelper;
import com.studyyoun.android.nonghe.welcom.model.UserModel;

/**
 * Created by androidlongs on 2017/8/27.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

public class PersonFragment extends CommonBaseFragment {

    private LinearLayout mPersonHeaderLinearLayout;
    private ItemHeader mItemHeader;
    private ImageView mSettingImageView;
    private LinearLayout mMyReadHistoryLayout;
    private LinearLayout mMyVideoReadHistoryLayout;
    private RecyclerView mJpushRecyclerView;
    private ItemJpushMsg mItemJpushMsg;
    private LinearLayout mMyCollectLinearLayout;

    @Override
    protected int getCommonLayoutId() {
        return R.layout.fragment_person_layout;
    }

    @Override
    protected void commonInitView(View view) {

        mPersonHeaderLinearLayout = view.findViewById(R.id.rl_person_header);
        mSettingImageView = view.findViewById(R.id.iv_person_header_setting);
        mMyReadHistoryLayout = view.findViewById(R.id.ll_my_read_history);
        mMyVideoReadHistoryLayout = view.findViewById(R.id.ll_my_read_video_history);

        mJpushRecyclerView = view.findViewById(R.id.rv_jpush_show);

        mMyCollectLinearLayout = view.findViewById(R.id.ll_my_collect);
    }

    @Override
    protected void commonFunction() {
        //头部功能设置
        mItemHeader = new ItemHeader(mPersonHeaderLinearLayout);

        //推送消息设置
        mItemJpushMsg = new ItemJpushMsg(mContext, mJpushRecyclerView, mHandler);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mItemHeader != null) {
            mItemHeader.dimissLoginPage();
        }
        if (mItemJpushMsg != null) {
            mItemJpushMsg.loadingLocationDatas();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mItemJpushMsg != null) {
            mItemJpushMsg.stopMsgFunction();
        }
    }

    @Override
    protected void commonDelayFunction() {
        mSettingImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.getInstance().startActivity(new Intent(App.getInstance(), SettingActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            }
        });
        mMyReadHistoryLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserModel userModel = CommonUserHelper.getUserModel();
                if (userModel == null) {
                    ToastUtils.show("请登录", mContext);
                } else {
                    App.getInstance().startActivity(new Intent(App.getInstance(), MyArtReadHistoryActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                }
            }
        });
        mMyVideoReadHistoryLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserModel userModel = CommonUserHelper.getUserModel();
                if (userModel == null) {
                    ToastUtils.show("请登录", mContext);
                } else {
                    App.getInstance().startActivity(new Intent(App.getInstance(), MyVideoReadHistoryActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                }
            }
        });
        mMyCollectLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserModel userModel = CommonUserHelper.getUserModel();
                if (userModel == null) {
                    ToastUtils.show("请登录", mContext);
                } else {
                    App.getInstance().startActivity(new Intent(App.getInstance(), MyCollectActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                }
            }
        });
        if (mItemJpushMsg != null) {
            mItemJpushMsg.loadingLocationDatas();
        }
    }

    public void loadingLoacation() {
        if (mItemJpushMsg != null) {
            mItemJpushMsg.loadingLocationDatas();
        }
    }

}
